﻿using Api.BLL;
using Api.Common;
using Api.Model;
using Api.Web.filter;
using Api.Web.helper;
using System;
using System.Linq;
using System.Web.Mvc;

namespace Api.Web.Areas.Admin.Controllers
{
    [IsAdmin]
    public class UserController : AdminController
    {
        public ActionResult List()
        {
            var list = UserBLL.Current.GetListNoTrack(null);
            if (list == null)
            {
                return View("_500");
            }
            return View(list);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Edit(User user)
        {
            bool flag = true;
            if (!string.IsNullOrEmpty(user.LoginName))
            {
                if (user.Id > 0)
                {
                    try
                    {
                        UserBLL.Current.UpdateIgnore(s => new { s.CreateDate, }, user);
                    }
                    catch
                    {
                        flag = false;
                    }
                }
                else
                {
                    user.CreateDate = DateTime.Now;
                    try
                    {
                        UserBLL.Current.Add(user);
                    }
                    catch
                    {
                        flag = false;
                    }
                }

                if (flag)
                {
                    AjaxMsgHelper.ResponseSuccess("", null, Url.Action("List", new { Area = "Admin" }));
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗，請重試");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請填寫登錄名或登錄密碼");
            }
        }
        public ActionResult Create()
        {
            ViewBag.Title = "創建用戶";
            return View();
        }
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                ViewBag.Error = "參數錯誤";
                return View("Create");
            }
            User user = UserBLL.Current.GetModel(s => s.Id == id);
            if (user == null)
            {
                ViewBag.Error = "數據不存在";
                return View("Create");
            }

            ViewBag.Title = "用戶修改";
            return View("Create", user);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Delete(params int[] ids)
        {
            if (ids.Length > 0)
            {
                var list = UserBLL.Current.GetList(s => ids.Contains(s.Id));
                if (list == null)
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                    return;
                }
                bool result = true;
                try
                {
                    UserBLL.Current.DeleteList(list);
                }
                catch
                {
                    result = false;
                }
                if (result)
                {
                    AjaxMsgHelper.ResponseSuccess("刪除成功");
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請選擇一項");
            }
        }
    }
}